//package com.majun.file.util;
//
//import com.amazonaws.SdkClientException;
//import com.amazonaws.services.s3.AmazonS3;
//import com.amazonaws.services.s3.model.*;
//import com.amazonaws.util.IOUtils;
//import com.amazonaws.util.StringUtils;
//import com.su.config.AwzS3ProtiesConfig;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.MediaType;
//import org.springframework.http.ResponseEntity;
//import org.springframework.stereotype.Component;
//import org.springframework.web.multipart.MultipartFile;
//
//import javax.annotation.Resource;
//import java.io.IOException;
//import java.net.URLEncoder;
//import java.util.Objects;
//
//@Component
//public class AwzS3UtilBack {
//
//    @Resource
//    private AwzS3ProtiesConfig injectAwzS3ProtiesConfig;
//
//    private static AwzS3ProtiesConfig awzS3ProtiesConfig;
//
//    private static AmazonS3 amazonS3;
//
//
////    @PostConstruct
////    public void init() {
////        awzS3ProtiesConfig = this.injectAwzS3ProtiesConfig;
////        AWSCredentials credentials = new BasicAWSCredentials(awzS3ProtiesConfig.getAccessKey(), awzS3ProtiesConfig.getSecretKey());
////        AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials);
////        AwsClientBuilder.EndpointConfiguration endpointConfig =
////                new AwsClientBuilder.EndpointConfiguration(awzS3ProtiesConfig.getUrl(), Regions.CN_NORTH_1.getName());
////        ClientConfiguration config = new ClientConfiguration();
////        config.setSignerOverride("S3SignerType");
////        config.setProtocol(Protocol.HTTP);
////        config.withUseExpectContinue(false);
////        config.disableSocketProxy();
////        amazonS3 = AmazonS3Client.builder()
////                .withEndpointConfiguration(endpointConfig)
////                .withClientConfiguration(config)
////                .withCredentials(credentialsProvider)
////                .disableChunkedEncoding()
////                .withPathStyleAccessEnabled(true)
////                .withForceGlobalBucketAccessEnabled(true)
////                .build();
////    }
//
//
//    /**
//     * 创建桶
//     *
//     * @return boolean
//     * @params * @param bucket
//     * @author susj
//     * @date 2022/12/12 16:39
//     */
//    public static boolean createBucket(String bucket) {
//        if (StringUtils.isNullOrEmpty(bucket)) {
//            throw new IllegalArgumentException("桶名称不能为空!");
//        }
//        try {
//            amazonS3.createBucket(bucket);
//        } catch (SdkClientException e) {
//            e.printStackTrace();
//            return false;
//        }
//        return true;
//    }
//
//    /**
//     * 删除桶
//     *
//     * @return boolean
//     * @params * @param bucket
//     * @author susj
//     * @date 2022/12/12 16:39
//     */
//    public static boolean deleteBucket(String bucket) {
//        if (StringUtils.isNullOrEmpty(bucket)) {
//            throw new IllegalArgumentException("桶名称不能为空!");
//        }
//        try {
//            amazonS3.deleteBucket(bucket);
//        } catch (SdkClientException e) {
//            e.printStackTrace();
//            return false;
//        }
//        return true;
//    }
//
//    /**
//     * 上传文件
//     *
//     * @param bucket 桶名称
//     * @return boolean
//     * @params * @param file
//     * @author susj
//     * @date 2022/12/12 16:33
//     */
//    public static boolean upload(MultipartFile file, String bucket) {
//        if (Objects.isNull(file)) {
//            throw new IllegalArgumentException("文件不能为空!");
//        }
//        String fileName = file.getOriginalFilename();
//        ObjectMetadata objectMetadata = new ObjectMetadata();
//        objectMetadata.setContentType(file.getContentType());
//        objectMetadata.setContentLength(file.getSize());
//        bucket = StringUtils.isNullOrEmpty(bucket) ? awzS3ProtiesConfig.getBucket() : bucket;
//        try {
//            PutObjectRequest request = new PutObjectRequest(bucket, fileName, file.getInputStream(), objectMetadata)
//                    // 设置文件访问权限
//                    .withCannedAcl(CannedAccessControlList.BucketOwnerFullControl);
//            PutObjectResult result = amazonS3.putObject(request);
//        } catch (IOException e) {
//            e.printStackTrace();
//            return false;
//        }
//        return true;
//    }
//
//
//    /**
//     * 通过文件名称下载
//     *
//     * @param fileName
//     * @return org.springframework.http.ResponseEntity<byte [ ]>
//     * @params * @param bucket
//     * @author susj
//     * @date 2022/12/12 16:56
//     */
//    public static ResponseEntity<byte[]>    downloadByName(String bucket, String fileName) throws IOException {
//        if (StringUtils.isNullOrEmpty(bucket)) {
//            throw new IllegalArgumentException("文件名称不能为空!");
//        }
//        bucket = StringUtils.isNullOrEmpty(bucket) ? awzS3ProtiesConfig.getBucket() : bucket;
//        GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, fileName);
//        S3Object s3Object = amazonS3.getObject(getObjectRequest);
//        S3ObjectInputStream objectInputStream = s3Object.getObjectContent();
//        byte[] bytes = IOUtils.toByteArray(objectInputStream);
//        // 替换中文
//        String showFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
//        HttpHeaders httpHeaders = new HttpHeaders();
//        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//        httpHeaders.setContentLength(bytes.length);
//        httpHeaders.setContentDispositionFormData("attachment", showFileName);
//        return new ResponseEntity<>(bytes, httpHeaders, HttpStatus.OK);
//    }
//
//
//    /**
//     * 删除单个文件
//     *
//     * @param fileName
//     * @return boolean
//     * @params * @param bucket
//     * @author susj
//     * @date 2022/12/12 17:04
//     */
//    public static boolean deleteFile(String bucket, String fileName) {
//        if (StringUtils.isNullOrEmpty(fileName)) {
//            throw new IllegalArgumentException("文件名称不能为空!");
//        }
//        bucket = StringUtils.isNullOrEmpty(bucket) ? awzS3ProtiesConfig.getBucket() : bucket;
//        try {
//            amazonS3.deleteObject(bucket, fileName);
//        } catch (SdkClientException e) {
//            e.printStackTrace();
//            return false;
//        }
//        return true;
//    }
//
//    /**
//     * 获取文件信息
//     *
//     * @param fileName
//     * @return com.amazonaws.services.s3.model.ObjectMetadata
//     * @params * @param bucket
//     * @author susj
//     * @date 2022/12/12 17:08
//     */
//    public static ObjectMetadata getFileInfo(String bucket, String fileName) {
//        if (StringUtils.isNullOrEmpty(fileName)) {
//            throw new IllegalArgumentException("文件名称不能为空!");
//        }
//        bucket = StringUtils.isNullOrEmpty(bucket) ? awzS3ProtiesConfig.getBucket() : bucket;
//        ObjectMetadata objectMetadata = null;
//        try {
//            objectMetadata = amazonS3.getObjectMetadata(bucket, fileName);
//        } catch (SdkClientException e) {
//            e.printStackTrace();
//        }
//        return objectMetadata;
//    }
//
//
//}
